/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Certification;
import com.applicatioN.entities.Recevoir;
import com.applicatioN.entities.RecevoirPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class RecevoirJpaController implements Serializable {

    public RecevoirJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Recevoir recevoir) throws PreexistingEntityException, Exception {
        if (recevoir.getRecevoirPK() == null) {
            recevoir.setRecevoirPK(new RecevoirPK());
        }
        recevoir.getRecevoirPK().setOrganisme(recevoir.getCertification().getCertificationPK().getOrganisme());
        recevoir.getRecevoirPK().setIdentifiant(recevoir.getCandidat().getIdentifiant());
        recevoir.getRecevoirPK().setDomaineDExpertise(recevoir.getCertification().getCertificationPK().getDomaineDExpertise());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Certification certification = recevoir.getCertification();
            if (certification != null) {
                certification = em.getReference(certification.getClass(), certification.getCertificationPK());
                recevoir.setCertification(certification);
            }
            em.persist(recevoir);
            if (certification != null) {
                certification.getRecevoirCollection().add(recevoir);
                certification = em.merge(certification);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findRecevoir(recevoir.getRecevoirPK()) != null) {
                throw new PreexistingEntityException("Recevoir " + recevoir + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Recevoir recevoir) throws NonexistentEntityException, Exception {
        recevoir.getRecevoirPK().setOrganisme(recevoir.getCertification().getCertificationPK().getOrganisme());
        recevoir.getRecevoirPK().setIdentifiant(recevoir.getCandidat().getIdentifiant());
        recevoir.getRecevoirPK().setDomaineDExpertise(recevoir.getCertification().getCertificationPK().getDomaineDExpertise());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Recevoir persistentRecevoir = em.find(Recevoir.class, recevoir.getRecevoirPK());
            Certification certificationOld = persistentRecevoir.getCertification();
            Certification certificationNew = recevoir.getCertification();
            if (certificationNew != null) {
                certificationNew = em.getReference(certificationNew.getClass(), certificationNew.getCertificationPK());
                recevoir.setCertification(certificationNew);
            }
            recevoir = em.merge(recevoir);
            if (certificationOld != null && !certificationOld.equals(certificationNew)) {
                certificationOld.getRecevoirCollection().remove(recevoir);
                certificationOld = em.merge(certificationOld);
            }
            if (certificationNew != null && !certificationNew.equals(certificationOld)) {
                certificationNew.getRecevoirCollection().add(recevoir);
                certificationNew = em.merge(certificationNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                RecevoirPK id = recevoir.getRecevoirPK();
                if (findRecevoir(id) == null) {
                    throw new NonexistentEntityException("The recevoir with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(RecevoirPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Recevoir recevoir;
            try {
                recevoir = em.getReference(Recevoir.class, id);
                recevoir.getRecevoirPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The recevoir with id " + id + " no longer exists.", enfe);
            }
            Certification certification = recevoir.getCertification();
            if (certification != null) {
                certification.getRecevoirCollection().remove(recevoir);
                certification = em.merge(certification);
            }
            em.remove(recevoir);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Recevoir> findRecevoirEntities() {
        return findRecevoirEntities(true, -1, -1);
    }

    public List<Recevoir> findRecevoirEntities(int maxResults, int firstResult) {
        return findRecevoirEntities(false, maxResults, firstResult);
    }

    private List<Recevoir> findRecevoirEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Recevoir.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Recevoir findRecevoir(RecevoirPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Recevoir.class, id);
        } finally {
            em.close();
        }
    }

    public int getRecevoirCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Recevoir> rt = cq.from(Recevoir.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
